from datetime import datetime
from storages.backends.gcloud import GoogleCloudStorage
import os

from rest_framework.exceptions import ValidationError

storage = GoogleCloudStorage()


def extension(filename):
    name, extension = os.path.splitext(filename)
    return extension


class ImageUpload:

    @staticmethod
    def upload_image(file, file_type, id, filename):
        if file:
            target_path = file_type + '/' + id + str(datetime.now()) + filename
            path = storage.save(target_path, file)
            return storage.url(path)
        raise ValidationError("Image is required")
